Ext.define('PJ.view.celaquestionnairemanage.QuestionnaireManageList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.celaQuestionnaireManageList'],
    requires: ['AM.default.DefaultSearchForm', 
               'AM.default.DateTime', 
               'AM.default.DefaultDateField',
               'AM.default.jsonp.DefaultLoadJsonp',
               'PJ.view.celaquestionnairemanage.SetQuestionsFrame'//设置试题
               ],
    
    isPossibleLoad: true,
    id: 'celaQuestionnaireManageList',
    
    initComponent: function(){
    	var me = this;
        me.columns = [{
            header: '问卷名称',
            dataIndex: 'q.questionnaireName',
            flex: 1,
            minWidth: 120,
			renderer: function(value, metaData, record){
				metaData.tdAttr = 'data-qtip="' + value + '"';
				return value;
			}
        }/*, {
            header: '开始时间',
            dataIndex: 'q.beginDate',
            width: 130,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '结束时间',
            dataIndex: 'q.endDate',
            width: 130,
            renderer: function(value, metaData, record){
                return value;
            }
        }*/, {
            header: '问卷说明',
            dataIndex: 'questionnaireExplain',
            flex: 1,
            minWidth: 120,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '创建时间',
            dataIndex: 'q.createDate',
            width: 100,
            renderer: function(value, metaData, record){
                return value;
            }
        }, {
            header: '发布状态',
            dataIndex: 'q.publishState',
            width: 70,
            renderer: function(value, metaData, record){
                if(value == 1){
                	return "已发布";
                }else if(value == 2){
                	return "未发布";
                }
            }
        }/*, {
        	header:"操作",
        	width: 60,
        	renderer: function(value, meta, record){
        		meta.style = "overflow:visible;";
                var searchQuestionnaireId = record.get("questionnaireId");
                var publishState = record.get("q.publishState");
                var url = me.store.viewPaperUrl;
                var saUrl = me.store.saUrl;
                return "<span onclick=\"settingCelaQuestions('"+searchQuestionnaireId+"','"+publishState+"')\"  class='configLocationIcon' data-qtip='设置试题' style='cursor: pointer;width: 40px;background-repeat: no-repeat;margin-left:3px;'>"+
		                	"&nbsp;&nbsp;&nbsp;&nbsp;试题"+
		               "</span>";
            }
        }*/];
        me.store = Ext.create('PJ.'+ prototype +'.celaquestionnairemanage.QuestionnaireManageList', {
            isLoad: false
        });
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        me.callParent(arguments);
    },
    
    //新增
    onAddClick: function(button){
        var me = this;
        var win = Ext.create('PJ.view.celaquestionnairemanage.QuestionnaireManageEdit', {
            grid: this,
            notEdit: true,
            title: "新增问卷"
        }).show();
    },
    
    //更新
    onUpdateClick: function(button){
    	var me = this;
        var record = this.getSelectionModel().getSelection();
        var searchPublishState = record[0].get("q.publishState");
        if (searchPublishState == 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '不可修改已发布的问卷！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
        var win = Ext.create('PJ.view.celaquestionnairemanage.QuestionnaireManageEdit', {
            grid: this,
            isEdit: false,
            notEdit: false,
            title: "更新问卷"
        }).show();
        var actionName = "load";
        if(gj_onlineLearning_constant.loadType=="jsonp") {
        	actionName= "loadJsonp";
        }
        win.down('form').getForm().doAction(actionName,{
            url: me.store.perUpdateUrl,
            params: {
            	questionnaireId: record[0].get("questionnaireId")
            },
            success:function(form,action){
            	//调用事件： 数据返回给编辑器中
//                win.setEditValue();
            }
        });
    },
    // 删除
    onDeleteClick: function(button){
        var me = this;
        var records = me.getSelectionModel().getSelection();
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("q.publishState") == 1) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '不可删除已发布的问卷！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要删除这些数据吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    var questionnaireIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	questionnaireIds.push(name.get("questionnaireId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    	    url: me.getStore().deleteUrl,
                    	    extraParams: {
                    	    	searchQuestionnaireIds: questionnaireIds
                    	    },
                    	    loadType:gj_onlineLearning_constant.loadType
                    	});
                    	store.load(function(rec, operation, success){
                    		var json = {
                    				 data :operation.response.data,
                    				 msg:operation.response.msg
                    			};
                    	     if (success) {
                    	    	 Ext.Msg.alert('消息', '删除成功！', function(){
                                     var store = me.getStore();
                                     store.reload();
                                 });
                    	     }else {
                    	    	 Ext.Msg.alert('消息', json.msg, function(){
                                     var store = me.getStore();
                                     store.reload();
                                 });
                    	    }
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                            url: me.getStore().deleteUrl,
                            params: {
                            	searchQuestionnaireIds: questionnaireIds
                            },
                            success: function(response){
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
                                    Ext.Msg.alert('消息', '删除成功！', function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                                else {
                                    Ext.Msg.alert('消息', json.msg, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            }
                        });
                    }
                }
                else {
                    return;
                }
            }
        });
    },
    /**
     * 设置试题
     * @param button
     */
    onConfigLocationClick: function(button){
    	var me = this;
        var records = me.getSelectionModel().getSelection();
        if(records.length > 1){
        	Ext.Msg.show({
                title: '提示',
                msg: '一次只可操作一条数据！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }else{
        	var searchQuestionnaireId = records[0].get("questionnaireId");
            var publishState = records[0].get("q.publishState");
            var url = me.store.viewPaperUrl;
            var saUrl = me.store.saUrl;
            settingCelaQuestions(searchQuestionnaireId,publishState);
        }
        
    	
    },
 // 发布
    onPublishClick: function(button){
        var me = this;
        var records = me.getSelectionModel().getSelection();
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("q.publishState") == 1) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '不可对已发布的数据再次执行发布操作！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
            if (records[i].get("isSetStaff") == 2) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '设置调查对象时未指定人员不可执行发布操作！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要执行发布操作吗?',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                        var questionnaireIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionnaireIds.push(name.get("questionnaireId"));
                        });
                        var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
        					loadType: gj_onlineLearning_constant.loadType,
        					url: me.getStore().publishUrl,
        					extraParams: {
        						searchQuestionnaireIds: questionnaireIds,
        						publishState: 1
        					}
        				});
        				store.load(function(rec, operation, success){
        					if(success){
        						Ext.Msg.alert('消息', operation.response.msg || '发布成功！', function(){
                                    var store = me.getStore();
                                    store.reload();
                                });
        					}else{
        						Orchid.alert('消息', operation.error);
        					}
        				});
                    }
                    else {
                        return;
                    }
                }
            });
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要执行发布操作吗？',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                        var questionnaireIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionnaireIds.push(name.get("questionnaireId"));
                        });
                        Ext.Ajax.request({
                            url: me.getStore().publishUrl,
                            params: {
                            	searchQuestionnaireIds: questionnaireIds,
        						publishState: 1
                            },
                            success: function(response){
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
                                    Ext.Msg.alert('消息', json.msg || '发布成功！', function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                                else {
                                    Ext.Msg.alert('消息', json.msg, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            }
                        });
                    }
                    else {
                        return;
                    }
                }
            });
        }
    },
    
 // 撤销发布
    onUnpublishClick: function(button){
        var me = this;
        var records = me.getSelectionModel().getSelection();
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("q.publishState") == 2) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '不可对未发布的数据执行撤销发布操作！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
            if (records[i].get("started") == 1) {
                Ext.Msg.show({
                    title: '提示',
                    msg: '调查问卷已开始，不能撤销发布！',
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要执行撤销发布操作吗?',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                        var questionnaireIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionnaireIds.push(name.get("questionnaireId"));
                        });
                        var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
        					loadType: gj_onlineLearning_constant.loadType,
        					url: me.getStore().unPublishUrl,
        					extraParams: {
        						searchQuestionnaireIds: questionnaireIds,
        						publishState: 2
        					}
        				});
        				store.load(function(rec, operation, success){
        					if(success){
        						Ext.Msg.alert('消息', operation.response.msg || '撤销发布成功！', function(){
                                    var store = me.getStore();
                                    store.reload();
                                });
        					}else{
        						Orchid.alert('消息', operation.error);
        					}
        				});
                    }
                    else {
                        return;
                    }
                }
            });
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Msg.confirm({
                title: '提示',
                msg: '您确定要执行撤销发布操作吗？',
                buttons: Ext.Msg.OKCANCEL,
                fn: function(id){
                    if (id == 'ok') {
                        var questionnaireIds = new Array();
                        Ext.Array.each(records, function(name, index){
                        	questionnaireIds.push(name.get("questionnaireId"));
                        });
                        Ext.Ajax.request({
                            url: me.getStore().unPublishUrl,
                            params: {
                            	searchQuestionnaireIds: questionnaireIds,
        						publishState: 2
                            },
                            success: function(response){
                                var data = response.responseText;
                                var json = Ext.decode(data, true);
                                if (json.success) {
                                    Ext.Msg.alert('消息', json.msg || '撤销发布成功！', function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                                else {
                                    Ext.Msg.alert('消息', json.msg, function(){
                                        var store = me.getStore();
                                        store.reload();
                                    });
                                }
                            }
                        });
                    }
                    else {
                        return;
                    }
                }
            });
        }
    },
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Publish").setDisabled(selected.length === 0);
			this.down('#' + this.oldId + "Unpublish").setDisabled(selected.length === 0);
			this.down('#' + this.oldId + "ConfigLocation").setDisabled(selected.length === 0);
        },
    	viewready: function(){
    		this.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 3,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 70
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '问卷名称',
    				name: "searchQuestionnaireName",
    				maxLength: "50",
    				emptyText: "请输入问卷名称"
    			}, {
    				xtype: "combo",
    				fieldLabel: '发布状态',
    				name: "searchPublishState",
    				store: Ext.create("Ext.data.ArrayStore", {
    					fields: ['value', 'text'],
    					data: [[1, '已发布'],[2,'未发布']]
    				}),
    				valueField: 'value',
    				displayField: 'text',
    				editable: false,
    				emptyText: "请选择"
    			}, {
    				xtype: "defaultDate",
    				fieldLabel: '创建时间',
    				name: "searchCreateDate",
    				maxLength: "20",
    				emptyText: "请输入创建时间"
    			}/*, {
    				xtype: 'defaultDate',
    				fieldLabel: '开始时间',
    				name: 'searchStartTime',
    				id: 'celaQuestionnaireManageSD',
    				vtype: 'startLessEndDate',
    				startLessEndDate: {
    					targetCmpId: 'celaQuestionnaireManageED',
    					isStart: true
    				},
    				emptyText: "请输入开始时间"
    			}, {
    				xtype: 'defaultDate',
    				fieldLabel: '结束时间',
    				name: 'searchEndTime',
    				id: 'celaQuestionnaireManageED',
    				vtype: 'startLessEndDate',
    				vtypeText: '结束时间不能早于开始时间！',
    				startLessEndDate: {
    					targetCmpId: 'celaQuestionnaireManageSD',
    					isStart: false
    				},
    				emptyText: "请输入结束时间"
    			}*/],
    			grid: this
    		}, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                    text: "新增问卷",
                    iconCls: 'addIco',
                    itemId: this.oldId + "Add",
                    scope: this,
                    handler: function(button){
                        button.up("grid").onAddClick(button);
                    }
                }, "-", {
                    text: "更新问卷",
                    iconCls: 'editIco',
                    itemId: this.oldId + "Update",
                    disabled: true,
                    handler: function(button){
                        if (button.up("grid").getSelectionModel().getCount() > 1) {
                            Ext.Msg.show({
                                title: orchid_lang.promptWindowInfo.warningTitle,
                                msg: default_lang.defaultList.operatingWarningMsg,
                                icon: Ext.Msg.WARNING,
                                buttons: Ext.Msg.OK
                            });
                            return;
                        }
                        else {
                            button.up("grid").onUpdateClick(button);
                        }
                    }
                }, "-", {
                    text: "删除问卷",
                    disabled: true,
                    itemId: this.oldId + "Delete",
                    iconCls: 'deleteIco',
                    handler: function(button){
                        button.up("grid").onDeleteClick(button);
                    }
                }, "-", {
                    text: "设置试题",
                    disabled: true,
                    itemId: this.oldId + "ConfigLocation",
                    iconCls: 'configLocationIcon',
                    handler: function(button){
                        button.up("grid").onConfigLocationClick(button);
                    }
                }, "-", {
                    text: '发布',
                    iconCls: 'publishIco',
                    itemId: this.oldId + "Publish",
                    disabled: true, 
                    scope: this,
                    handler: this.onPublishClick
                }, "-", {
                    text: '撤销发布',
                    iconCls: 'publishCancelIco',
                    itemId: this.oldId + "Unpublish",
                    disabled: true,
                    scope: this,
                    handler: this.onUnpublishClick
                }, "->", this.countSelectPanel]
            }]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		 var store = this.store;
             var params = store.proxy.extraParams;
             var newParams = {};
             Ext.apply(params, newParams);
             store.isLoad = true;
             store.load();
    	}
    }
});

